package com.edu.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.security.config.DBUserDetailManager;
import com.edu.security.entity.User;
import com.edu.security.mapper.UserMapper;
import com.edu.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private DBUserDetailManager dbUserDetailManager;

    @Override
    public void saveUserDetails(User user) {

        // 创建 UserDetails 对象
        UserDetails userDetails =
                org.springframework.security.core.userdetails.User
                        .withDefaultPasswordEncoder()
                        .username(user.getUsername())
                        .password(user.getPassword())
                        .build();

//                        .withDefaultPasswordEncoder()
//                        .username(user.getUsername())
//                        .password(user.getPassword())
//                        .build();
        dbUserDetailManager.createUser(userDetails);
    }
}
